package com.lg.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import java.util.Map;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class FirstFragment extends Fragment {
    String TAG = FirstFragment.class.getSimpleName();

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                NavHostFragment.findNavController(FirstFragment.this)
//                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
                testRxJava();
                //testCreateRxJava();
            }
        });
    }

   void testRxJava(){
        String path="urlxxx";

       Observable.just(path).map(new Function<String, String>() {

           @Override
           public String apply(String s) throws Throwable {
               Log.d(TAG, "apply: "+s);
                String result =  " apply = "+s;
               return result;
           }
       })    .subscribeOn(Schedulers.io()) // todo  给上游分配 异步线程
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Observer<String>(){

                   @Override
                   public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
                       Log.d(TAG, "onSubscribe: ");

                   }

                   @Override
                   public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                       Log.d(TAG, "onNext: "+s);

                   }

                   @Override
                   public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                       Log.d(TAG, "onError: "+e.getLocalizedMessage());

                   }

                   @Override
                   public void onComplete() {
                       Log.d(TAG, "onComplete: ");

                   }
               });
    }

    void testCreateRxJava(){
        Observable.create(new ObservableOnSubscribe<Integer>() {

            @Override
            public void subscribe(@io.reactivex.rxjava3.annotations.NonNull ObservableEmitter<Integer> emitter) throws Throwable {
                Log.d(TAG, "subscribe: ");

            }
        }).subscribe(new Observer<Integer>(){

            @Override
            public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
                Log.d(TAG, "onSubscribe: ");

            }

            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull Integer integer) {
                Log.d(TAG, "onNext: ");

            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                Log.d(TAG, "onError: ");

            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete: ");

            }
        });
    }
    
    void testCusJava(){
        String path="testcusJava";
        Observable.just(path).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
                Log.d(TAG, "onSubscribe: ");

            }

            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                Log.d(TAG, "onNext: ");

            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                Log.d(TAG, "onError: ");

            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete: ");

            }
        });
//        com.lg.customRxJava.Observable.just(path).map(new com.lg.customRxJava.Function<String, Object>() {
//            @Override
//            public String apply(String s) throws Throwable {
//                Log.d(TAG, "apply: "+s);
//                String result =  " apply = "+s;
//                return result;
//            }
//        }).subscribe(new com.lg.customRxJava.Observer<String>(){
//
//             @Override
//             public void onSubscribe(com.lg.customRxJava.Disposable d) {
//
//             }
//
//             @Override
//             public void onNext(String s) {
//
//             }
//
//             @Override
//             public void onError(Throwable e) {
//
//             }
//
//             @Override
//             public void onComplete() {
//
//             }
//         });
 }

    void testMap(){
        Observable.just("asdf").map(new Function<String, Object>() {
            @Override
            public Object apply(String s) throws Throwable {
                return null;
            }
        }).subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull Object o) {

            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}